package org.ycx.doc.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ycx.common.constants.RespCode;
import org.ycx.common.dto.RespMSG;
import org.ycx.doc.dao.enums.GroupUsersRole;
import org.ycx.doc.dao.model.DocGroup;
import org.ycx.doc.dao.model.DocProject;
import org.ycx.doc.service.IDocGroupService;
import org.ycx.doc.service.IDocProjectService;
import org.ycx.upms.common.constants.SecurityConstants;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController {

    @Autowired
    private IDocGroupService groupService;

    @Autowired
    private IDocProjectService projectService;

    @RequestMapping("/indexData")
    @ResponseBody
    public RespMSG homeData(HttpSession session) {
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        RespMSG resp = new RespMSG ();
        Map<String, Object> data = new HashMap<String, Object> ();
        List<DocGroup> groups = this.groupService.findGroupByUser (uid, GroupUsersRole.Master);
        List<GroupUsersRole> roles = new ArrayList<GroupUsersRole> ();
        roles.add (GroupUsersRole.Master);
        List<DocProject> projects = this.projectService.findProjectByUser (uid, roles);
        data.put ("groups", groups);
        data.put ("projects", projects);
        resp.initResp (RespCode.SUCCESS);
        resp.setData (data);
        return resp;
    }

    @RequestMapping("/indexNavData")
    @ResponseBody
    public RespMSG navData(HttpSession session) {
        int uid = (int) session.getAttribute (SecurityConstants.CURRENT_UID);
        RespMSG resp = new RespMSG ();
        Map<String, Object> data = new HashMap<String, Object> ();
        List<DocGroup> groups = this.groupService.findGroupByUser (uid, GroupUsersRole.Master);
        groups.addAll (this.groupService.findGroupByUser (uid, GroupUsersRole.Developer));
        groups.addAll (this.groupService.findGroupByUser (uid, GroupUsersRole.Guest));
        List<GroupUsersRole> roles = new ArrayList<GroupUsersRole> ();
        roles.add (GroupUsersRole.Master);
        roles.add (GroupUsersRole.Developer);
        roles.add (GroupUsersRole.Guest);
        List<DocProject> projects = this.projectService.findProjectByUser (uid, roles);
        data.put ("groups", groups);
        data.put ("projects", projects);
        resp.initResp (RespCode.SUCCESS);
        resp.setData (data);
        return resp;
    }
}
